package at.ac.tuwien.isis.infret.ue2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;

import at.ac.tuwien.isis.infret.helper.DocumentContainer;
import at.ac.tuwien.isis.infret.ue2.interfaces.IArffParser;

public class ArffParser implements IArffParser {
	
	//private Vector<DocumentContainer> documentContainer;
	Instances data;

	public ArffParser(String path){
		try {
			BufferedReader reader = new BufferedReader(new FileReader(path));
			ArffReader arff = new ArffReader(reader);
			data = arff.getData();
			data.setClassIndex(data.numAttributes() - 1);
			//System.out.println("Opened");
			//System.out.println(data.classAttribute());
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	@Override
	public Vector<DocumentContainer> getDocumentContainer() {
		
		Vector<DocumentContainer> dc = new Vector<DocumentContainer>();
		
		Enumeration<Instance> enumerate =  data.enumerateInstances();
		while(enumerate.hasMoreElements()){
			Instance entry = enumerate.nextElement();
			//System.out.println(entry.toString(0));
			//System.out.println(entry.toString(1));
			dc.add(new DocumentContainer(entry.stringValue(1), entry.stringValue(0), entry));
			//if(dc.size()%100 == 0){
			//	System.out.print("=");
			//}
		}
		
		//System.out.println("=>");
		return dc;
	}

}
